﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test1
{
    class OrderService
    {
        private List<Order> orders = new List<Order>();
        public OrderService() { }
        public OrderService(List<Order> orders)
        {
            this.orders = orders;
        }
        /**
         * 按总金额排序
         */
        public void sortByPrice()
        {
            orders.Sort((p1, p2) => p1.Sum.CompareTo(p2.Sum));
        }
        /**
         * 添加订单
         */
        public void add(Order order)
        {
            if (!order.Equals(querybyOrderId(order.Id)))
            {
                orders.Add(order);
            }
            else
            {
                Console.WriteLine("添加失败，已有相同订单!");
            }
        }
        /**
         * 根据订单号删除订单
         */
        public void remove(int id)
        {
            try
            {
                foreach (Order order in orders)
                {
                    if (order.Id == id)
                    {
                        orders.Remove(order);
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("订单删除失败!");
                Console.WriteLine(e.Message);
            }
        }
        /**
         * 根据订单号修改订单
         */
        public void update(int id, Order newOrder)
        {
            try
            {
                foreach (Order order in orders)
                {
                    if (order.Id == id)
                    {
                        orders.Remove(order);
                        orders.Add(newOrder);
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("订单修改失败!");
                Console.WriteLine(e.Message);
            }
        }
        /**
         * 按照订单号查询
         */
        public Order querybyOrderId(int id)
        {
            var queryResult = orders.Where(obj => obj.Id == id).FirstOrDefault();
            return queryResult;
        }
        /**
         * 按照买家姓名查询
         */
        public List<Order> querybyBuyerName(string buyerName) 
        {
            var queryResult = orders.Where(obj => obj.Buyer.Name == buyerName).OrderBy(p => p.Sum);
            if (queryResult != null)
            {
                return queryResult.ToList();
            }
            else
            {
                return new List<Order>();
            }
        }
        public void outPut()
        {
            try
            {
                foreach (Order order in orders)
                {
                    Console.WriteLine(order.ToString());
                }
            }
            catch(Exception e)
            {
                Console.WriteLine("遍历失败!");
                Console.WriteLine(e.Message);
            }
        }
    }
}
